import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["roleCards"];

/**
 * model 工作台配置
 * @date: 2018-9-25


 */
import { getResponse } from "../utils/utils";
import { queryIdpValue } from "../services/api";
import { workplaceLayoutQuery, // 查询当前的 Layout
workplaceLayoutUpdate, // 保存当前编辑的 Layout
workplaceCardsQuery // 查询当前 拥有的卡片
} from "../services/workplaceService";
export default {
  namespace: 'workplace',
  state: {
    prevRoleCards: undefined,
    // 前一个可分配卡片
    roleCards: [],
    // 可分配卡片
    catalogType: [] // 卡片类型

  },
  effects: {
    fetchLayoutAndInit: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLayoutAndInit(_, _ref) {
      var call, put, all, allRequest, _yield$all, _yield$all2, layout, roleCards, catalogType, realLayout, realCatalogType, realRoleCards, nextState;

      return _regeneratorRuntime.wrap(function fetchLayoutAndInit$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put, all = _ref.all;
              allRequest = [call(workplaceLayoutQuery), call(workplaceCardsQuery), call(queryIdpValue, 'CPFM.DASHBOARD_CARD.TYPE')];
              _context.next = 4;
              return all(allRequest);

            case 4:
              _yield$all = _context.sent;
              _yield$all2 = _slicedToArray(_yield$all, 3);
              layout = _yield$all2[0];
              roleCards = _yield$all2[1];
              catalogType = _yield$all2[2];
              realLayout = getResponse(layout);
              realCatalogType = getResponse(catalogType);
              realRoleCards = getResponse(roleCards);
              nextState = {};

              if (realRoleCards) {
                nextState.roleCards = realRoleCards;
              }

              if (realCatalogType) {
                nextState.catalogType = realCatalogType;
              }

              _context.next = 17;
              return put({
                type: 'updateState',
                payload: nextState
              });

            case 17:
              return _context.abrupt("return", realLayout);

            case 18:
            case "end":
              return _context.stop();
          }
        }
      }, fetchLayoutAndInit);
    }),
    fetchLayout: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLayout(_, _ref2) {
      var call, res;
      return _regeneratorRuntime.wrap(function fetchLayout$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call;
              _context2.next = 3;
              return call(workplaceLayoutQuery);

            case 3:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchLayout);
    }),
    saveLayout: /*#__PURE__*/_regeneratorRuntime.mark(function saveLayout(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveLayout$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context3.next = 4;
              return call(workplaceLayoutUpdate, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveLayout);
    }),
    fetchCards: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCards(_ref5, _ref6) {
      var payload, call, put, roleCards, params, res, realRes;
      return _regeneratorRuntime.wrap(function fetchCards$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              roleCards = payload.roleCards, params = _objectWithoutProperties(payload, _excluded);
              _context4.next = 5;
              return call(workplaceCardsQuery, params);

            case 5:
              res = _context4.sent;
              realRes = getResponse(res);

              if (!realRes) {
                _context4.next = 10;
                break;
              }

              _context4.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  prevRoleCards: roleCards,
                  roleCards: realRes
                }
              });

            case 10:
              return _context4.abrupt("return", realRes);

            case 11:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchCards);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};